from xgboost import XGBClassifier
import pandas as pd
import numpy as np




#读入数据
print('######### data reading ###########')
data = pd.read_csv("train_FE_6.csv")
train = data.sample(frac=0.25)
train_GBDT = data.sample(frac=0.2)
print('####################')
print('train_original_shape', train.shape)
print('####################')
print('train_GBDT_shape', train_GBDT.shape)

train = train.reset_index(drop=True)

Cat_features_MEncoder = ['site_id','site_domain','app_id','app_domain','device_id','device_ip','device_model','C14','C17','C20']

#抽取要训练特征
y_train = train['click']
X_train = train.drop(np.hstack((Cat_features_MEncoder,['click'])), axis=1)
print('##################')
print('X_train_shape', X_train.shape)

y_train_GBDT = train_GBDT['click']
X_train_GBDT = train_GBDT.drop(np.hstack((Cat_features_MEncoder,['click'])), axis=1)
print('##################')
print('X_train_GBDT_shape', X_train_GBDT.shape)

#GBDT训练
#params = {"objective": "multi:softprob", "eval_metric":"mlogloss", "num_class": 9}
xgb1 = XGBClassifier(
        learning_rate =0.1,
        n_estimators=35,  #数值大没关系，cv会自动返回合适的n_estimators
        max_depth=5,
        min_child_weight=1,
        gamma=0,
        subsample=0.5,
        colsample_bytree=1,
        objective= 'binary:logistic',
        seed=3)

xgb1.fit(X_train_GBDT, y_train_GBDT, eval_metric='logloss')
Cat_features_GBDT = xgb1.apply(X_train)
Cat_features_GBDT = pd.DataFrame(Cat_features_GBDT)


Cat_features_GBDT = Cat_features_GBDT.astype(str)
Cat_features_GBDT = pd.get_dummies(Cat_features_GBDT)
print('##################')
print('Cat_features_GBDT_shape', Cat_features_GBDT.shape)



newTrain = pd.concat([train,Cat_features_GBDT],axis=1)
print('##################')
print('newdata_shape', newTrain.shape)

newTrain = newTrain.drop(Cat_features_MEncoder, axis=1)
#newTrain = newTrain.sample(frac=0.25)
print('##################')
print('newdata_shape_after drop', newTrain.shape)

print('########begin saving#######')
newTrain.to_csv('FE_GBDT.csv',index=False)
print('########begin finished#######')




